var loginApp = angular.module('loginApp',[])
loginApp.controller('loginController',function($scope,$http){
    //发送的方法
    $scope.formData = {};
    $scope.postForm = function(){
        //1.formDate添加一个属性，action代表请求的行为
        $scope.formData.action='login';
        $http({
            method:'POST',
            url:'./get.php',
            data: $.param($scope.formData),
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
        }).success(function(data){
            console.log(data);
            if(!data.success){
                //当前登录失败
                if(!data.errors){
                    //用户名密码错误
                    $scope.message = data.message;
                }else{
                    //用户名密码为空
                    $scope.errorUsername = data.errors.username;
                    $scope.errorPassword = data.errors.password;
                }
            }else{
                window.location.href = '#list/0';
            }
        })
    }
})
var pageList = angular.module('pageList',[]);
pageList.controller('ListTypeCtrl',function($scope,$http){
    $http({
        method:'GET',
        url:'get.php?action=get_arctype&where=reid=0'
    }).success(function(data){
        $scope.lists = data;
    }).error(function(err){
        console.log(err);
    })
})
//文章列表的控制器
totals = 0;
pageList.controller('arcListCtrl',['$scope','$http','$location',function($scope,$http,$location) {
    $scope.typeid = $location.path().replace('/', '');
    console.log($scope.typeid);
    if ($scope.typeid == 0) {
        //获取所有文章
        $get_total_url = 'get.php?action=get_total';
    } else {
        //获取某一个分类的文章的数量
        $get_total_url = 'get.php?action=get_total&where=typeid=' + $scope.typeid;
    }
    //获取所有的对应分类的文章数量
    $http({
        method: 'GET',
        url: $get_total_url
    }).success(function (data) {
        //当前文章的总数量
        $scope.paginationConf.totalItems = data.total;
    }).error(function (err) {
        console.log(err);
    })
    //设置分页的对象
    $scope.paginationConf = {
        //当前页数
        currentPage: 1,
        //每页多少条
        itemsPerPage: 5,
        //totalItems 文章的总数量
        pageslength: 5,
        //可以自动更换的每页多少条
        perPageOptions: [10, 20, 30, 40, 50],
        rememberPerPage: 'perPageItems',
        onChange:function() {
            //先判断一个当前页数是否是第一页，获取到查询的起始位置
            if ($scope.paginationConf.currentPage == 1) {
                //当前是第一页
                $scope.limit = 0;
            } else {
                //(当前页数 - 1）*每页多少条
                $scope.limit = $scope.paginationConf.currentPage * $scope.paginationConf.itemsPerPage - $scope.paginationConf.itemsPerPage;
            }
            //根据typeid和当前的页数currentPage整理好请求的url
            if ($scope.typeid == 0) {
                $geturl = 'get.php?action=get_list&offset=' + $scope.limit + '&rows='
                    + $scope.paginationConf.itemsPerPage + '&orderField=id&orderBy=DESC';
            } else {
                $geturl = 'get.php?action=get_list&offset=' + $scope.limit + '&rows='
                    + $scope.paginationConf.itemsPerPage + '&where=typeid=' + $scope.typeid + '&orderField=id&orderBy=DESC';
            }
            //最后发送请求
            // offset 当前查询的偏移量
            // rows  当前查询多少条
            // typeid  查询什么类型的文章  安卓  ios
            // scope.limit  查询条过几条
            // order by 排序方式

            $http({
                method: 'GET',
                url: $geturl
            }).success(function (data) {
                $scope.lists = data;
                console.log(data);
            }).error(function (err) {
                console.log(err);
            })
        }
    }
}])